import type from "./action-type";
import * as Api from "api/api";

export const setPageSize = payload => {
  return {
    type: type.SET_PAGE_SIZE,
    payload
  };
};

export const setPageNum = payload => {
  return {
    type: type.SET_PAGE_NUM,
    payload
  };
};

export const setTotalNum = payload => {
  return {
    type: type.SET_TOTAL_NUM,
    payload
  };
};

export const setList = payload => {
  return {
    type: type.SET_LIST,
    payload
  };
};

export const setName = payload => {
  return {
    type: type.SET_NAME,
    payload
  };
};

export const setAccount = payload => {
  return {
    type: type.SET_ACCOUNT,
    payload
  };
};

export const setStartDate = payload => {
  return {
    type: type.SET_START_DATE,
    payload
  };
};

export const setEndDate = payload => {
  return {
    type: type.SET_END_DATE,
    payload
  };
};

export const setLoading = payload => {
  return {
    type: type.SET_LOADING,
    payload
  };
};

export const fetchList = ({
  pageSize,
  pageNum,
  name,
  account,
  startDate,
  endDate
}) => {
  return async f => {
    f(setLoading(true));
    const res = await Api.getWithdrawList({
      pageSize,
      pageNum,
      name,
      account,
      startDate,
      endDate
    });
    f(setLoading(false));

    if (res.status === 200) {
      let { data } = res;
      f(
        setList(
          data.msg.objectList.map(item => ({
            ...item,
            key: item.id,
            money: `${item.money / 100} 元`
          }))
        )
      );
      f(setTotalNum(data.msg.totalNum));
    }
  };
};
